<!--产值统计-->
<template>
  <div class="com-container">
    <div class="com-chart" ref="chanzhi_ref"></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      chartInstance: null,
      allData: null, //服务器返回的数据
      startValue: 0, //区域缩放起点值
      endValue: 4, //区域缩放终点值
      timeId: null, //定时器id
    };
  },
  mounted() {
    this.initChart();
    this.getData();
    window.addEventListener("resize", this.screenAdapter);
    this.screenAdapter();
  },
  destroyed() {
    window.removeEventListener("resize", this.screenAdapter);
    clearInterval(this.timeId);
  },
  methods: {
    initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.chanzhi_ref, "chalk");
      const initOption = {
        tooltip: {
          show: true,
        },
        // title: {
        //   text: "▎ 产值统计",
        //   left: 20,
        //   top: 0,
        // },

        backgroundColor: "", //设置无背景色
        grid: {
          top: "15%",
          left: "5%",
          right: "5%",
          bottom: "8%",
          containLabel: true, //距离包含坐标轴文字
        },
        xAxis: {
          type: "category",
          axisTick: {
            alignWithLabel: true,
          },
          //修改刻度标签相关样式
          axisLabel: {
            color: "#fff",
          },
        },
        yAxis: {
          type: "value",
          axisLabel: {
            color: "#fff",
          },
          // y坐标轴的线的样式
          axisLine: {
            lineStyle: {
              color: "rgba(255,255,255,.1)",
              //   默认
              width: 2,
              // type: "solid"
            },
          },
          // y轴分割线的颜色
          splitLine: {
            lineStyle: {
              color: "rgba(255,255,255,.1)",
            },
          },
        },
        series: [
          {
            type: "bar",
          },
        ],
      };
      this.chartInstance.setOption(initOption);
      // 对图表进行鼠标事件的监听
      this.chartInstance.on("mouseover", () => {
        console.log("鼠标移入");
        this.timeId && clearInterval(this.timeId);
      });
      this.chartInstance.on("mouseout", () => {
        console.log("鼠标移出");
        this.startInterval();
      });
    },
    async getData() {
      const res = await this.$http.get("/sbst/sbst_number");
      this.allData = res.data;
      // console.log(this.allData);
      this.updateChart();
      this.startInterval();
    },
    updateChart() {
      const colorArr = [
        ["#0BA82C", "#4FF778"],
        ["#2E72BF", "#23E5E5"],
        ["#5052EE", "#AB6EE5"],
      ];
      const chanzhi = this.allData;
      // console.log(chanzhi);
      const dataOption = {
        xAxis: {
          data: ["输电", "变电", "送电", "停电"],
        },
        //向右移动
        // dataZoom: {
        //   show: false,
        //   startValue: this.startValue,
        //   endValue: this.endValue,
        // },
        series: [
          {
            data: chanzhi,
            itemStyle: {
              color: (arg) => {
                let targetColorArr = null;
                if (arg.value > 400) {
                  targetColorArr = colorArr[0];
                } else if (arg.value > 300) {
                  targetColorArr = colorArr[1];
                } else {
                  targetColorArr = colorArr[2];
                }
                return new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: targetColorArr[0],
                  },
                  {
                    offset: 1,
                    color: targetColorArr[1],
                  },
                ]);
              },
            },
          },
        ],
      };
      this.chartInstance.setOption(dataOption);
    },
    screenAdapter() {
      const titleFontSize = (this.$refs.chanzhi_ref.offsetWidth / 100) * 3.6;
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize * 1.5,
          },
        },
        series: [
          {
            barWidth: titleFontSize,
            itemStyle: {
              barBorderRadius: [titleFontSize / 2, titleFontSize / 2, 0, 0],
            },
          },
        ],
      };
      this.chartInstance.setOption(adapterOption);
      this.chartInstance.resize();
    },
    startInterval() {
      if (this.timeId) {
        clearInterval(this.timeId);
      }
      this.timeId = setInterval(() => {
        this.startValue = this.startValue + 1;
        this.endValue = this.endValue + 1;
        if (this.endValue > this.allData.length - 1) {
          this.startValue = 0;
          this.endValue = 4;
        }
        this.updateChart();
      }, 2000);
    },
  },
};
</script>

<style>
</style>